package edu.bbu.cshu.wmpcWebGUI;

import java.io.IOException;
import java.util.ArrayList;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import edu.bbu.cshu.wmpc.plagiarism.PlagiarismTest;

/**
 * Servlet implementation class LoadDatasServlet
 */
@WebServlet("/LoadDatasServlet")
public class LoadDatasServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
        
	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 * a jsp-rol jovo keresek feldolgozasa, ujabb adatok beallitasa a sessionbe
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		HttpSession sess = request.getSession(true);
		
		//a feluleten valasztott file tipus lekerdezese
		String fileFormat[] = request.getParameterValues("fileFormat");
		int n;
		try{
			n = fileFormat.length;
		}catch( Exception e){
			n = 0;
		}
		
		String radioValue;
		if (n > 0) {
			radioValue = (String)request.getParameter("fileFormat");
		}else{ //alapertelmezett file format, ha nem volt kivalasztva semmi
			radioValue = PlagiarismTest.INCTANCE.getFileTypes().get(0);
		}
		
		sess.setAttribute("Error", null);
		sess.setAttribute("showCombos", 1);  //most mar lesz combohoz valo lista
		
		ArrayList<String> datas = new ArrayList<String>();
		PlagiarismTest.INCTANCE.configureConnection("localhost", 3306, "file", "root", "");
		try {
			datas = PlagiarismTest.INCTANCE.getFileInfo(radioValue);
		} catch (Exception e) {
			sess.setAttribute("Error", true);
			sess.setAttribute("ErrorText", 1);
			sess.setAttribute("showCombos", 0);
			datas.add("Error");
		}finally{
			if (datas.isEmpty()){
				sess.setAttribute("Error", true);
				sess.setAttribute("ErrorNr", 1);
				sess.setAttribute("showCombos", 0);
				datas.add("Error");
			}
		
		}
		
		ArrayList<String> testTypesList = PlagiarismTest.INCTANCE.getTestTypes();
		ArrayList<String> ctp = new ArrayList<String>();
		ctp.add(testTypesList.get(0));
		ArrayList<String> levelTypesList = PlagiarismTest.INCTANCE.getLevelTypes();
		
		sess.setAttribute("fFormat", radioValue);
		sess.setAttribute("datas", datas);
		sess.setAttribute("fFileN", datas.get(0));
		sess.setAttribute("sFileN", datas.get(0));
		sess.setAttribute("lTypes", levelTypesList.get(0));
		sess.setAttribute("cTypes",ctp); 
		sess.setAttribute("showResults", 0);
		request.getRequestDispatcher("Main.jsp").forward(request, response);
	}

}
